package com.sxyc.weixin.controller;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.AccessToken;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.api.JsTicketApi.JsApiType;
import com.jfinal.weixin.sdk.cache.IAccessTokenCache;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.sxyc.utils.WeixinUtil;

public class OAuth2 extends ApiController {

	static IAccessTokenCache accessTokenCache = ApiConfigKit.getAccessTokenCache();
	
	public ApiConfig getApiConfig() {
		return WeixinUtil.getApiConfig();
	}
	
	public void authorize(){
		String code = getPara("code");
    	if(code == null){
    		String redirect_uri = getPara("redirect_uri");
    		String scope = getPara("scope");
    		String state = getPara("state");
			System.out.println("get redirect_uri = "+redirect_uri+"   state = "+state+"   scope = "+scope);
           
			// 存储 session 数据
			getRequest().getSession().setAttribute("redirect_uri", redirect_uri);
			getRequest().getSession().setAttribute("state", state);
			
            //根据scope选择授权模式
			boolean snsapiBase = false;
            if("snsapi_base".equals(scope)){
            	snsapiBase = true;
            }
    		String url = SnsAccessTokenApi.getAuthorizeURL(PropKit.get("appId"), PropKit.get("domain")+"/OAuth2/authorize", snsapiBase);
			redirect(url);
		}else{
			String state = "wx";
			if(getRequest().getSession().getAttribute("state") != null) {
				state = (String) getRequest().getSession().getAttribute("state");
			}
			String url = getRequest().getSession().getAttribute("redirect_uri")+"?code="+code+"&state="+state;
			redirect(url);
		}
	}
	
	public void gettoken(){
		String appId = ApiConfigKit.getApiConfig().getAppId();
		AccessToken result = accessTokenCache.get(appId);
		if (result != null && result.isAvailable()){
			System.out.println("ok");
		}else{
			AccessTokenApi.refreshAccessToken();
			result = accessTokenCache.get(appId);
		}
		
		JSONObject res = new JSONObject();
		res.put("expiredTime",result.getExpiredTime());
		res.put("accessToken",result.getAccessToken());
		renderJson(res);
	}
	
	public void getticket(){
		JsTicket jsApiTicket = JsTicketApi.getTicket(JsApiType.jsapi);
		String jsapi_ticket = jsApiTicket.getTicket();
		long expiredTime = 0;
		//利用反射机制获取JsTicket类中的private属性expiredTime
		try {
			Field field = jsApiTicket.getClass().getDeclaredField("expiredTime");
			field.setAccessible(true);
			expiredTime = (long) field.get(jsApiTicket);
		} catch (NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JSONObject res = new JSONObject();
		res.put("expiredTime", expiredTime);
		res.put("accessToken", jsapi_ticket);
		renderJson(res);
	}

}
